<!-- 电子班牌编辑弹窗 -->
<template>
  <el-dialog
      :title="isUpdate?'修改电子班牌':'添加电子班牌'"
      :visible="visible"
      width="850px"
      :destroy-on-close="true"
      :lock-scroll="false"
      @update:visible="updateVisible">
    <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px">
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item label="所属学校:" prop="organization">
            <el-select
                filterable
                clearable
                v-model="form.organization"
                @change="changeGradeList"
                size="small"
                placeholder="-请选择所属学校-"
                class="ele-block">
              <el-option v-for="item in schoolList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item label="年级:" prop="grade">
            <el-select
                filterable
                clearable
                v-model="form.grade"
                @change="changeClazzList"
                size="small"
                placeholder="-请选择年级-"
                class="ele-block">
              <el-option v-for="item in gradeList" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-tooltip class="item" effect="dark" content="提示：班级与所属场地必须二选一，非班级场地班牌可选所属场地" placement="top">
        <el-col :lg="12" :md="12">
      <el-form-item label="班级:" prop="class_id">
          <el-select
            filterable
            clearable
            v-model="form.class_id"
            @change="$forceUpdate()"
            size="small"
            placeholder="-请选择班级 -"
            class="ele-block">
            <el-option v-for="item in clazzList" :key="item.id" :label="item.name" :value="item.id"/>
          </el-select>
      </el-form-item>
      </el-col>
      </el-tooltip>
      <el-tooltip class="item" effect="dark" content="提示：班级与所属场地必须二选一，非班级场地班牌可选所属场地" placement="top">
      <el-col :lg="12" :md="12">
      <el-form-item label="所属场地:" prop="scene_id">
        <el-select
          filterable
          clearable
          v-model="form.scene_id"
          @change="$forceUpdate()"
          size="small"
          placeholder="-请选择所属场地-"
          class="ele-block">
          <el-option v-for="item in sceneList" :key="item.id" :label="item.name" :value="item.id"/>
        </el-select>
      </el-form-item>
      </el-col>
      </el-tooltip>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item
              label="班牌名称:"
              prop="name">
            <el-input
                :maxlength="20"
                v-model="form.name"
                placeholder="请输入班牌名称"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item
              label="设备编码:"
              prop="code">
            <el-input
                :maxlength="20"
                v-model="form.code"
                placeholder="请输入设备编码"
                clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="12" :md="12">
          <el-form-item
              label="账号:"
              prop="account">
            <el-input
                :maxlength="20"
                v-model="form.account"
                placeholder="请输入账号"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="12" :md="12">
          <el-form-item
              label="密码:"
              prop="password">
            <el-input
                :maxlength="20"
                v-model="form.password"
                placeholder="请输入密码"
                clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-col :lg="8" :md="12">
          <el-form-item
              label="班级口号:"
              prop="kouhao">
            <el-input
                :maxlength="20"
                v-model="form.kouhao"
                placeholder="请输入班级口号"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item
              label="班级目标:"
              prop="target">
            <el-input
                :maxlength="20"
                v-model="form.target"
                placeholder="请输入班级目标"
                clearable/>
          </el-form-item>
        </el-col>
        <el-col :lg="8" :md="12">
          <el-form-item
              label="班级格言:"
              prop="geyan">
            <el-input
                :maxlength="20"
                v-model="form.geyan"
                placeholder="请输入格言"
                clearable/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="15">
        <el-form-item
            label="轮播图:"
            prop="pic_data">
          <uploadImage :limit="10" v-model="form.pic_data"></uploadImage>
        </el-form-item>
      </el-row>
      <el-row :gutter="15">
        <el-form-item label="在线状态:">
          <el-radio-group
              v-model="form.online">
            <el-radio :label="0">离线</el-radio>
            <el-radio :label="1">在线</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-row>
      <el-row :gutter="15">
        <el-form-item label="显示顺序:" prop="sort">
          <el-input-number
              :min="0"
              v-model="form.sort"
              placeholder="请输入显示顺序"
              controls-position="right"
              class="ele-fluid ele-text-left"/>
        </el-form-item>
      </el-row>
      <el-row :gutter="15">
        <el-form-item label="备注:">
          <el-input
              :rows="3"
              clearable
              type="textarea"
              :maxlength="200"
              v-model="form.note"
              placeholder="请输入备注"/>
        </el-form-item>
      </el-row>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
          type="primary"
          @click="save"
          :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import uploadImage from '@/components/uploadImage'

export default {
  name: 'EquipEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  components: {uploadImage},
  data() {
    return {
      // 表单数据
      form: Object.assign({status: 1}, this.data),
      // 表单验证规则
      rules: {
        organization: [
          {required: true, message: '请输入所属学校', trigger: 'blur'}
        ],
        grade: [
          {required: true, message: '请输入所属年级', trigger: 'blur'}
        ],
        // class_id: [
        //   {required: true, message: '请输入所属班级', trigger: 'blur'}
        // ],
        //           scene_id: [
        //   {required: true, message: '请输入所属场地', trigger: 'blur'}
        // ],
                  name: [
          {required: true, message: '请输入班牌名称', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入设备编码', trigger: 'blur'}
        ],
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入显示顺序', trigger: 'blur'}
        ],
        note: [
          {required: true, message: '请输入备注', trigger: 'blur'}
        ],
        pic_data: [
          {required: true, message: '请上传轮播图', trigger: 'blur'}
        ],
      },
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      // 学院列表
      schoolList: [],
      // 年级列表
      gradeList: [],
      // 班级列表
      clazzList: [],
      // 场地列表
      sceneList: [],
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.getSchoolList();
        this.isUpdate = true;
      } else {
        this.form = {};
        this.isUpdate = false;
      }
    }
  },
  mounted() {
    // 获取学院列表
    this.getSchoolList();
    // 获取负责人列表
    // this.getSceneList();
  },
  methods: {
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true;
          if (typeof this.form.pic_data !== 'undefined') {
            this.form.pic_data = JSON.stringify(this.form.pic_data.map(num => num.toString()));
          } else {
            this.form.pic_data = [];
          }
          this.$http.post('/equip/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    },
    /**
     * 获取组织列表
     */
    getSchoolList() {
      if (this.form.organization) {
        // this.form.superior = null;
        this.$forceUpdate();
        this.getGradeList();
        this.getSceneList();
      }
      this.$http.post('/school/getSchoolList').then(res => {
        if (res.data.code === 0) {
          this.schoolList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    getGradeList() {
      if (this.form.organization) {
        // this.form.grade = null;
        this.getClazzList();
        this.$forceUpdate();
      }
      this.$http.post('/grade/getGradeList', {oid: this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.gradeList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取班级列表
     */
    getClazzList() {
      if (this.form.grade) {
        // this.form.grade = null;
        this.$forceUpdate();
      }
      this.$http.post('/clazz/getClazzList', {gid: this.form.grade}).then(res => {
        if (res.data.code === 0) {
          this.clazzList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取场地列表
     */
    getSceneList() {
      if (this.form.scene_id) {
        // this.form.scene_id = null;
        this.$forceUpdate();
      }
      this.$http.post('/sitescene/getSceneList', {oid: this.form.organization}).then(res => {
        if (res.data.code === 0) {
          this.sceneList = res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /**
     * 获取年级列表
     */
    changeGradeList() {
      this.form.grade = null;
      this.form.class_id = null;
      this.form.scene_id = null;
      this.getGradeList();
      this.getSceneList();
    },
    /**
     * 获取场地列表
     */
    changeClazzList() {
      this.form.class_id = null;
      this.getClazzList();
    },
  }
}
</script>

<style scoped>
</style>
